//
//  KLSellerViewController.m
//  used
//
//  Created by xalo on 16/7/20.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "KLSellerViewController.h"
@interface KLSellerViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

/** 卖家模型 */
@property (nonatomic, strong) KLSeller *sellerModel;

/** 在售物品 */
@property (nonatomic, strong) NSMutableArray *sellGoods;


@property (nonatomic, strong) NSMutableArray *goodsID;

@end

@implementation KLSellerViewController

- (NSMutableArray *)goodsID {
    if (!_goodsID) {
        _goodsID = [NSMutableArray array];
    }
    return _goodsID;
}

- (NSMutableArray *)sellGoods {
    if (!_sellGoods) {
        _sellGoods = [NSMutableArray array];
    }
    return _sellGoods;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = KLRGBColor(220, 220, 220);
    
    [self.view addSubview:self.tableView];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(5, 30, 40, 40)];
    [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    [self loadSellerData];
    
    [self loadSellGoodData];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YSLRecommendCell" bundle:nil] forCellReuseIdentifier:@"YSLRecommendModel_cell"];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)loadSellerData {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 10.0f;
    
    [manager POST:kSellerUrl parameters:@{@"userid":self.userID} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dictRoot = responseObject;
        self.sellerModel = [KLSeller mj_objectWithKeyValues:dictRoot[@"result"]];

        dispatch_async(dispatch_get_main_queue(), ^{
            
            KLSellerTopView *topView = [KLSellerTopView sellerView];
            topView.frame = CGRectMake(0, 0, kWidth, kWidth * 0.6);
            topView.seller = self.sellerModel;
            self.tableView.tableHeaderView = topView;
            
            // 结束刷新
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            [self.tableView reloadData];
            
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //        [SVProgressHUD showErrorWithStatus:@"加载失败"];
        // 结束刷新
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)loadSellGoodData {

    NSString *urlStr = [NSString stringWithFormat:@"http://m.jiuaixianzhi.com/api/v4/sellinggoods/ioscommon/%@/", self.userID];
    [RequestManager fetchDataWithUrl:urlStr parameter:nil method:@"GET" compeleted:^(NSData *data) {
       
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        self.sellGoods = [KLGood mj_objectArrayWithKeyValuesArray:dict[@"result"]];
        for (NSDictionary *dic in dict[@"result"]) {
            [self.goodsID addObject:dic[@"id"]];
        }
    } updateUI:^{
     
        [self.tableView reloadData];
    }];
}

- (void)back {
    [self dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sellGoods.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YSLRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YSLRecommendModel_cell" forIndexPath:indexPath];
    KLGood *model = self.sellGoods[indexPath.row];
    cell.model = model;

    cell.sellerMessageButton.userInteractionEnabled = YES;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 250;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YSLCommodityViewController *commodityVC = [[YSLCommodityViewController alloc] init];
  
    commodityVC.goodsID = self.goodsID[indexPath.row];

    [self.navigationController pushViewController:commodityVC animated:YES];
}



@end
